<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList(1)">
      <el-form-item>
        <el-select v-model="dataForm.keyword.status" @change="getDataList(1)">
        <el-option
          v-for="item in statusOptions"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
      </el-form-item>
      <el-form-item style="display: inline-block;">
        <el-date-picker
          v-model="dataForm.keyword.stime"
          type="datetime"
          value-format="timestamp"
          :picker-options="pickerOptionsStart"
          placeholder="选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item style="display: inline-block;">
        <el-date-picker
          v-model="dataForm.keyword.etime"
          type="datetime"
          value-format="timestamp"
          :picker-options="pickerOptionsEnd"
          placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(1)">查询</el-button>
        <el-button v-if="isAuth('picbook:feedback:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
        <el-table-column
            type="index"
            width="50"
        >
        </el-table-column>
      <el-table-column
        prop="imei"
        header-align="center"
        align="center"
        label="反馈手机序列号">
      </el-table-column>
      <el-table-column
        prop="pbUserId"
        header-align="center"
        align="center"
        label="用户ID">
      </el-table-column>
      <el-table-column
        prop="phone"
        header-align="center"
        align="center"
        label="用户手机号">
      </el-table-column>
      <el-table-column
        prop="contact"
        header-align="center"
        align="center"
        label="联系方式">
      </el-table-column>
      <el-table-column
        prop="content"
        header-align="center"
        align="center"
        label="问题描述">
      </el-table-column>
      <el-table-column
        prop="appVersion"
        header-align="center"
        align="center"
        label="版本号">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="反馈时间">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="处理状态">
        <template slot-scope="scope">
          <span>{{scope.row.status === 1? "已处理": "未处理"}}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <!-- <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button> -->
          <el-button type="text" size="small" @click="Handle(scope.row.id, scope.row.status)">{{scope.row.status === 1?"备注":"处理"}}</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <!-- <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update> -->
    <handle v-if="handleVisible" ref="handle" @refreshDataList="getDataList"></handle>
  </div>
</template>

<script>
  // import AddOrUpdate from './feedback-add-or-update'
  import Handle from './feedback-handle'
  export default {
    data () {
      return {
        dataForm: {
          keyword: {
            status: '',
            stime: '',
            etime: ''
          }
        },
        pickerOptionsStart: {
          disabledDate: time => {
            let endDateVal = this.dataForm.keyword.etime;
            if (endDateVal) {
              return time.getTime() > new Date(endDateVal).getTime();
            }
          }
        },
        pickerOptionsEnd: {
          disabledDate: time => {
            let beginDateVal = this.dataForm.keyword.stime;
            if (beginDateVal) {
              return (
                time.getTime() <
                new Date(beginDateVal).getTime() - 1 * 24 * 60 * 60 * 1000
              );
            }
          }
        },
        statusOptions: [{"id":'',"name":"全部"},{"id":0,"name":"未处理"},{"id":1,"name":"已处理"}],
        dataList: [],
        remark: '',
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        handleVisible: false
      }
    },
    components: {
      // AddOrUpdate
      Handle
    },
    activated () {
      this.getDataList()
      this.remark = ''
    },
    methods: {
      // 获取数据列表
      getDataList (formSearch) {
        // 搜索框来源
        if (formSearch === 1) {
          this.pageIndex = 1
        }
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/picbook/feedback/list'),
          method: 'post',
          data: this.$http.adornData({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keyword': this.dataForm.keyword
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      // addOrUpdateHandle (id) {
      //   this.addOrUpdateVisible = true
      //   this.$nextTick(() => {
      //     this.$refs.addOrUpdate.init(id)
      //   })
      // },
      // 处理
      Handle (id, status) {
        if (status == 0) {
          this.handleVisible = true
          this.$nextTick(() => {
            this.$refs.handle.init(id)
          })
        } else {
          this.$http({
            url: this.$http.adornUrl(`/picbook/feedback/info/${id}`),
            method: 'post'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.remark = data.data.feedback.remark
              this.$alert(`<div>处理备注:<div>${this.remark?this.remark:'无'}`, '反馈处理', {
                dangerouslyUseHTMLString: true
              });
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        // this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        this.$confirm(`确定[${id ? '删除' : '批量删除'}]?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/picbook/feedback/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
